<template>
    <div>
<van-nav-bar title="注册" />
<van-form @submit="onSubmit">
  <van-cell-group inset>
    <van-field
      v-model="username"
      name="用户名"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="password"
      :type="eye?'text':'password'"
      name="密码"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    >
  <template #button>
    <van-icon name="eye-o" style="font-size: 25px;" @click="eye=!eye"/>
  </template>
  </van-field>
    <van-field
      v-model="yzm"
      type="text"
      name="验证码"
      label="验证码"
      placeholder="验证码"
      :rules="[{ required: true, message: '请填写验证码' }]"
    />
     <van-button class="hqyzm" round block type="primary" @click="djyzm">
      发送验证码
    </van-button>
  </van-cell-group>
  <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">
      提交
    </van-button>
  </div>
</van-form>

    </div>
</template>

<script setup>
import { ref } from 'vue';
import { showToast } from 'vant';
import { loginApi } from "@/api/api";
import router from '@/router';
 const username = ref('');
    const password = ref('');
     const yzm = ref('');
    const onSubmit = () => {
      loginApi(username.value, password.value).then(res=>{
        localStorage.setItem('token',res.data.data.token)
        localStorage.setItem('username',username.value)
        console.log('dr',res);
        if(res.data.code==0){
            showToast({
                type:'success',
                message:'成功',
                duration:1500,
                onClose(){
                    router.push('/')

                }
            });

        }
      })
    };
    let time=ref(10)
    let end=0
    const djyzm=()=>{
      yzm.value=Math.floor(Math.random()*100000)
      end=setInterval(()=>{
        time.value--
        document.querySelector('.hqyzm').innerHTML=time.value+'秒'
        if(time.value==0){
          clearInterval(end)
          document.querySelector('.hqyzm').innerHTML='重新发送'
          time.value=10
        }
      },1000)

    }
    let eye=ref(false)
</script>

<style lang="scss" scoped>

</style>